using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace CMS_GYM.UserInterface
{
    public partial class cbDistrict : UserControl
    {
        public string Prov_ID;
        public string Dist_ID;
        public string Dist_Name;

        public cbDistrict()
        {
            InitializeComponent();
           // this.cbDist.isDistrict = true;
            this.Load += new EventHandler(cbDistrict_Load);
            cbDist.SelectedValueChanged += new EventHandler(cbDist_SelectedIndexChanged);
            //cbDist.isDistrict = true;
        }

        void cbDistrict_Load(object sender, EventArgs e)
        {
            //try
            //{
            //    if (this.cbDist.DataSource == null)
            //    {
            //        DataTable dt = null;
            //        if (Prov_ID != null)
            //            dt = new CMS_GYM.Address().dt_getDistrict(Prov_ID);
            //        else
            //            dt = new CMS_GYM.Address().dt_getDistrict();
            //        if (dt == null || dt.Rows.Count == 0) return;
            //        cbDist.DataSource = new DataView(dt);
            //        cbDist.DisplayMember = dt.Columns[2].ColumnName;
            //        cbDist.ValueMember = dt.Columns[0].ColumnName;
            //        cbDist_SelectedIndexChanged(sender, e);
            //    }
            //}
            //catch (Exception) { }
        }

        public void LoadData()
        {
            try
            {
                //if (this.cbDist.DataSource == null)
                //{
                    DataTable dt = null;
                    if (Prov_ID != null)
                        dt = new CMS_GYM.Address().dt_getDistrict(Prov_ID);
                    else
                        dt = new CMS_GYM.Address().dt_getDistrict();
                    if (dt == null || dt.Rows.Count == 0) return;
                    cbDist.DataSource = new DataView(dt);
                    cbDist.DisplayMember = dt.Columns[2].ColumnName;
                    cbDist.ValueMember = dt.Columns[0].ColumnName;
                    cbDist_SelectedIndexChanged(null, null);
                //}
            }
            catch (Exception) { }
        }
             

        void SelectedIndexChange()
        {
            if (cbDist.SelectedValue == null)
            {
                Dist_ID = "-1";
                Dist_Name = "";
                return;
            }
            Dist_ID = cbDist.SelectedValue == null ? string.Empty : cbDist.SelectedValue.ToString();
            Dist_Name = cbDist.Text;
        }
        void cbDist_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedIndexChange();
        }
        public void SetDistrict(string s_DistID)
        {

            if (s_DistID == "" || s_DistID == null || s_DistID == "NULL")
            {
                this.cbDist.SelectedValue = DBNull.Value;
                this.cbDist.Text = "";
            }
            else
            {
                this.cbDist.SelectedValue = s_DistID;

            }
        }
        public void SetDataSource(DataTable dtDist)
        {
            if (dtDist == null) return;
            cbDist.DataSource = dtDist;
            cbDist.DisplayMember = dtDist.Columns[2].ColumnName;
            cbDist.ValueMember = dtDist.Columns[0].ColumnName;
            SelectedIndexChange();
            if (dtDist.Rows.Count == 0)
                this.cbDist.SelectedValue=string.Empty;
        }
        public void setProvince(string sProvID, string LINK_SERVER)
        {
            //DataTable dt = Address.dt_getDistrict(sProvID);
            DataTable dt = new CMS_GYM.District().dt_getDistrict(sProvID);
            this.SetDataSource(dt);
        }
        public void loadDistrict(string LINK_SERVER)
        {
            //DataTable dt = Address.dt_getDistrict(sProvID);
            DataTable dt = new CMS_GYM.District().dt_loadDistrict();
            this.SetDataSource(dt);
        }
        //Created by anh.ltq
        public void loadDistrictByName(string LINK_SERVER)
        {
            DataTable dt = new CMS_GYM.District().dt_loadDistrictByName();
            this.SetDataSource(dt);
        }
        public void setProvince(string sProvID)
        {
            DataTable dt = new CMS_GYM.Address().dt_getDistrict(sProvID);
            this.SetDataSource(dt);
        }
    }
}
